# -*- coding:utf-8 -*-
import requests
import json


class OxfordApi(object):
    def __init__(self):
        # TODO: replace with your own app_id and app_key
        self.app_id = '1ba4436e'
        self.app_key = '55b7e425e08621723e3b6ecb6315d3f1'
        self.language = 'en'

    def get__root(self, word_id):
        url = 'https://od-api.oxforddictionaries.com:443/api/v1/inflections/' + self.language + '/' + word_id.lower()

        r = requests.get(url, headers={'app_id': self.app_id, 'app_key': self.app_key})

        r_dict = json.loads(json.dumps(r.json()))
        # print(r_dict)
        root = r_dict['results'][0]['lexicalEntries'][0]['inflectionOf'][0]['id']

        # print("code {}\n".format(r.status_code))
        # print("text \n%s"+r.text)
        # print("json \n" + json.dumps(r.json()))
        # print("root: {}".format(root))

        return root

    def get__synonym__antonym(self, word_id):
        url = 'https://od-api.oxforddictionaries.com:443/api/v1/entries/' + self.language + '/' + word_id.lower()+\
              '/synonyms;antonyms'

        r = requests.get(url, headers={'app_id': self.app_id, 'app_key': self.app_key})

        r_dict = json.loads(json.dumps(r.json(), indent=4))
        _synonym = [item['id'] for item in
                    r_dict['results'][0]['lexicalEntries'][0]['entries'][0]['senses'][0]['synonyms']]
        _antonym = [item['id'] for item in
                    r_dict['results'][0]['lexicalEntries'][0]['entries'][0]['senses'][0]['antonyms']]

        return (_synonym, _antonym)


if __name__ == '__main__':
    pass
